#include "Ally.h"
#include "Player.h"
#include "Ball.h"

CAlly::CAlly()
{
}


CAlly::~CAlly()
{
}

void CAlly::Update(float elapsedTime) /*override*/
{
	if (m_pPlayer->GetAmIInAMiniGame() == false)
	{
		if (m_pBall->IsHeld() == false)
		{
			m_tTable.Target = m_pBall;
			SwitchStates(CHASE_STATE);
		}
		else if (m_pBall->IsHeld() && m_pBall->GetOwner() != m_pPlayer->GetTeamMate() && m_pBall->GetOwner() != m_pPlayer)
		{
			m_tTable.Target = m_pBall->GetOwner();

			SwitchStates(CHASE_STATE);

			if (m_bInRange == true)
			{
				m_fAttackTimer = 0.5f;
				if (dynamic_cast<CPlayer*>(m_tTable.Target)->GetCurrStamina() / dynamic_cast<CPlayer*>(m_tTable.Target)->GetMaxStaMaxa() >= 0.5f)
					m_fTackleTimer = 1.5f;
				else if (dynamic_cast<CPlayer*>(m_tTable.Target)->GetCurrStamina() / dynamic_cast<CPlayer*>(m_tTable.Target)->GetMaxStaMaxa() >= 0.25f)
					m_fTackleTimer = 0.75f;

				if (m_fAttackTimer == 0.5f)
					dynamic_cast<CAgent*>(m_pPlayer)->Punch();

				if (m_fTackleTimer == 1.5f || m_fTackleTimer == 0.75f)
					m_pPlayer->Tackle();
			}
		}
		else if (m_pBall->IsHeld() && m_pBall->GetOwner() == m_pPlayer->GetTeamMate() && m_pBall->GetOwner() != m_pPlayer)
		{
			m_vtVelocity = { 0, 0 };
		}

		if (m_pPlayer->GetCarrying() == true)
		{
			if (m_pPlayer->GetTeam() == false)
				m_tTable.Target->SetPosition({ 736, 272 });
			else
				m_tTable.Target->SetPosition({ 69, 272 });
		}
	}
	else
	{

	}

	m_fAttackTimer -= elapsedTime;
	m_fTackleTimer -= elapsedTime;
	m_pPlayer->SetVelocity(m_vtVelocity);
	m_pPlayer->SetPosition(m_ptPosition);
	CAI::Update(elapsedTime);
}
